package yf.mall.dysdk.model.request.video;

import com.alibaba.fastjson.JSONObject;
import com.yf.response.RRException;
import com.yf.utils.HttpTookit;
import com.yf.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import yf.mall.utils.StringUtil;
import yf.mall.vo.app.dy.DyVideoVO;

import java.util.HashMap;
import java.util.Map;

/**
 * 猿推推的接口 需要token的 可能要经常换token
 * https://authtools-api.yuantuitui.com/api/command/link/ext
 */
@Slf4j
public class DyVideoRequest2 extends DyVideoBaseRequest {


    public DyVideoRequest2() {
        this.url = "https://authtools-api.yuantuitui.com/api/command/link/ext";
        this.method = HttpMethod.POST;
        this.token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJZSjhmVE5tSnlrS2JCQkhVNGFSdWZ3Iiwic2lkIjoiMjQyOTQwOCIsImF1ZCI6Ill0dEF1dGhvcmluZ1Rvb2xzQXBwIiwiaXNzIjoieXVhbnR1aXR1aS5jb20iLCJ0eXAiOiIxIiwibmJmIjoxNjkzNTc2MjI5LCJleHAiOjE2OTYxNjgyMjksImlhdCI6MTY5MzU3NjIyOX0.BR3QT-W88QVFB9QHs4HN1kT4M6KgGaCgFj48oDd5DmM";
        Map headers = new HashMap();
        headers.put("Authorization",this.token);
    }


    @Override
    public DyVideoVO parseVideo(String shareVideo) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("short_url", shareVideo);
        Long current = System.currentTimeMillis();
        String res = HttpTookit.doPostStr(url, jsonObject.toJSONString(),headers);
        current = System.currentTimeMillis() - current;
        log.info("请求去水印接口,url:{},res:{}", url, res);
        if (StringUtil.isBlank(res)) {
            log.error("请求去水印接口失败,url:{},res:{}", url, res);
            throw new RRException("去水印请求失败!");
        }
         jsonObject = JSONObject.parseObject(res);
        if (!jsonObject.get("code").equals(200) || !jsonObject.containsKey("data")) {
            log.error("请求去水印接口失败,url:{},res:{}", url, res);
            throw new RRException("去水印请求失败!");
        }
        JSONObject data = jsonObject.getJSONObject("data");
        if (!data.containsKey("result")) {
            log.error("请求去水印接口失败,url:{},res:{}", url, res);
        }
        data = data.getJSONObject("result");
        if (!data.containsKey("play_url")) {
            log.error("请求去水印接口失败,url:{},res:{}", url, res);
        }
        return new DyVideoVO(data.getString("play_url"), null, data.getString("cover"), data.getString("title"),current);
    }
}
